package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页查询 Brand数据
     *
     * @param page
     * @param rows
     * @param key
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key",required = false) String key
    ) {

        return ResponseEntity.ok(brandService.queryBrandByPage(page, rows,key));
    }

    /**
     * 根据 品牌id集合 查询品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(
            @RequestParam("ids") List<Long> ids
    ) {

        return ResponseEntity.ok(brandService.queryBrandByIds(ids));
    }

    /**
     * 根据分类id  查询品牌对象
     * @param c_id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(
            @RequestParam("id")Long c_id
    ) {

        return ResponseEntity.ok(brandService.queryBrandByCategory(c_id));
    }

    /**
     *  根据品牌id查询品牌对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {

        return ResponseEntity.ok(brandService.queryBrandById(id));
    }


    /**新增品牌
     *
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO) {
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新品牌 (修改)
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO) {

        brandService.updateBrand(brandDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id删除品牌 信息 包括中间表信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long id) {

        brandService.deleteBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

}
